package com.example.springbatchdemo.config.multi;

import com.example.springbatchdemo.config.file.Weather;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.BindException;

/**
 * @Classname MutiFileItemDemo
 * @Description TODO
 * @Date 2020/9/22 22:49
 * @Created by kevin
 */
@Configuration
public class MutiFileItemDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private MutiFileItemWriter mutiFileItemWriter;
    @Value("classpath:/data*.txt")
    private Resource[] fileResources;

    @Bean
    public Job mutiFileItemDemoJob() {

        return jobBuilderFactory.get("mutiFileItemDemoJob")
                .start(mutiFileItemDemoJobStep())
                .build();
    }

    @Bean
    public Step mutiFileItemDemoJobStep() {

        return stepBuilderFactory.get("mutiFileItemDemoJobStep")
                .<Weather, Weather>chunk(2)
                .reader(mutiFileItemReaer())
                .writer(mutiFileItemWriter)
                .build();
    }

    @Bean
    @StepScope
    public MultiResourceItemReader<Weather> mutiFileItemReaer() {
        MultiResourceItemReader<Weather> reader = new MultiResourceItemReader<>();
        reader.setResources(fileResources);
        reader.setDelegate(fileReaer());
        return reader;
    }

    @Bean
    @StepScope
    public FlatFileItemReader<Weather> fileReaer() {
        FlatFileItemReader<Weather> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("aaaa.txt"));
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"id", "month", "type", "value"});
        DefaultLineMapper<Weather> mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<Weather>() {
            @Override
            public Weather mapFieldSet( FieldSet fieldSet ) throws BindException {
                Weather weather = new Weather();
                weather.setId(fieldSet.readString("id"));
                weather.setMonth(fieldSet.readString("month"));
                weather.setType(fieldSet.readString("type"));
                weather.setValue(fieldSet.readString("value"));
                return weather;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        //reader.open(new ExecutionContext());
        return reader;
    }
}
